package cn.lzx.org.service.impl;

import cn.lzx.basic.service.impl.BaseServiceImpl;
import cn.lzx.basic.util.MD5Utils;
import cn.lzx.basic.util.StrUtils;
import cn.lzx.org.domain.Employee;
import cn.lzx.org.mapper.EmployeeMapper;
import cn.lzx.org.service.IEmployeeService;
import cn.lzx.user.domain.Logininfo;
import cn.lzx.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl <Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 信息同步到logininfo 添加
     * @param employee
     */
    @Override
    public void save(Employee employee) {
        //1.先保存t_employee对应的t_logininfo的信息 - 返回自增id
        //处理employee：添加salt，密码加密，设置type为员工0，然后保存
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        Logininfo info  = employee2Logininfo(employee);
        logininfoMapper.save(info);
        super.save(employee);
    }

    /**
     * 将employee对象同步到logininfo对象中
     * @param employee
     * @return
     */
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo info = new Logininfo();
        BeanUtils.copyProperties(employee,info);
        info.setType(0);
        return info;
    }
    /**
     * 删除同步
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
        //通过employee的id查询出empyloyee对象
        Employee employee = employeeMapper.loadById(id);
        //通过empyloyee对象查询出Logininfo_id
        Long logininfo_id = employee.getLogininfo_id();
        //根据Logininfo_id删除logininfo中对应的信息 因为 logininfo中的id就是employee中的logininfo_id
        logininfoMapper.delete(logininfo_id);
        super.delete(id);
    }

    /**
     * 修改信息同步
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        //1.先保存t_employee对应的t_logininfo的信息 - 返回自增id
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword()+salt);
        //处理employee：添加salt，密码加密，设置type为员工0，然后保存
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        //传入的employee是前端表单传过来的对象
        //因为传入的employee是没有logininfo_id的 我们通过主键查询出dbEmployee数据库中的employee
        Employee dbEmployee = employeeMapper.loadById(employee.getId());
        //通过数据库的 dbEmployee对象查询出他的logininfo_id
        Long logininfo_id = dbEmployee.getLogininfo_id();
        //同步信息的方法
        Logininfo logininfo = employee2Logininfo(employee);
        //因为employee中的logininfo_id就是logininfo中的id
        //所以我们把这个值赋值给logininfo对象
        logininfo.setId(logininfo_id);
        //通过这个对象来完成修改操作
        logininfoMapper.update(logininfo);
        super.update(employee);
    }
}
